<?php
class pembelian_model extends CI_Model{
	private $NoPo;
    private $Tanggalpo;
	private $Kodesupplier;
	private $suppliername;
	private $TotalQuantity;
    private $TotalAmount;
  	private $Keterangan;
	
	private $IDdetail;
	private $KodeDetailBarang;
	private $Productname;
	private $HargaBarang;
	private $Quantity;
    private $Discount;
    private $Amount;
    private $CreatedBy;
    private $CreatedOn;
    private $Lastupdateby;
  	private $Lastupdateon;
  	private $Active;
	
	private $tglAkhir;
	private $tglAwal;
	
	function setNoPo($NoPo){
		$this->NoPo = $NoPo;
	}
	function getNoPo(){
		return $this->NoPo;
	}
	
	function setTanggalpo(){
		$dateNow = date('Y-m-d');
		$this->Tanggalpo = $dateNow;
	}
	function getTanggalpo(){
		return $this->Tanggalpo;
	}
	
	function setKodesupplier($Kodesupplier){
		$this->Kodesupplier = $Kodesupplier;
	}
	function getKodesupplier(){
		return $this->Kodesupplier;
	}
	
	function setsuppliername($suppliername){
		$this->suppliername = $suppliername;
	}
	function getsuppliername(){
		return $this->suppliername;
	}
	
	function setTotalQuantity($ttlqty){
		$this->TotalQuantity = $ttlqty;
	}
	function getTotalQuantity(){
		return $this->TotalQuantity;
	}
	
	function setTotalAmount($ttlamount){
		$this->TotalAmount = $ttlamount;
	}
	function getTotalAmount(){
		return $this->TotalAmount;
	}
	
	function setKeterangan($ket){
		$this->Keterangan = $ket;
	}
	function getKeterangan(){
		return $this->Keterangan;
	}
	
	function setIDdetail($IDdetail){
		$this->IDdetail = $IDdetail;
	}
	function getIDdetail(){
		return $this->IDdetail;
	}
	
	function setKodeDetailBarang($kdbarangdet){
		$this->KodeDetailBarang = $kdbarangdet;
	}
	function getKodeDetailBarang(){
		return $this->KodeDetailBarang;
	}
	
	function setProductname($Productname){
		$this->Productname = $Productname;
	}
	function getProductname(){
		return $this->Productname;
	}
	
	function setHargaBarang($hrgbrg){
		$this->HargaBarang = $hrgbrg;
	}
	function getHargaBarang(){
		return $this->HargaBarang;
	}
	
	function setQuantity($QTY){
		$this->Quantity = $QTY;
	}
	function getQuantity(){
		return $this->Quantity;
	}
	
	function setDiscount($discount){
		$this->Discount = $discount;
	}
	function getDiscount(){
		return $this->Discount;
	}
	
	function setAmount($amount){
		$this->Amount = $amount;
	}
	function getAmount(){
		return $this->Amount;
	}
	
	function setCreatedBy($c_by){
		$this->CreatedBy = $c_by;
	}
	function getCreatedBy(){
		return $this->CreatedBy;
	}

	function setCreatedOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->CreatedOn = $dateNow;
	}
	function getCreatedOn(){
		return $this->CreatedOn;
	}

	function setLastUpdateBy($u_by){
		$this->LastUpdateBy = $u_by;
	}
	function getLastUpdateBy(){
		return $this->LastUpdateBy;
	}

	function setLastUpdateOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->LastUpdateOn = $dateNow;
	}
	function getLastUpdateOn(){
		return $this->LastUpdateOn;
	}

	function setActive($active_val){
		$this->Active = $active_val;
	}
	function getActive(){
		return $this->Active;
	}
	
	function setTanggalAwal($tglAwal){
		$this->tglAwal = $tglAwal;
	}
	function getTanggalAwal(){
		return $this->tglAwal;
	}
	
	function setTanggalAkhir($tglAkhir){
		$this->tglAkhir = $tglAkhir;
	}
	function getTanggalAkhir(){
		return $this->tglAkhir;
	}
	
	// Call the module constructor
	function __construct(){
		parent::__construct();
	}
	
	function getnopoCode()
	{
		$query = $this->db->query("select MAX(RIGHT(NoPo,4)) as kdnopo from order_product");
		$kd = "";
		
		if($query-> num_rows() > 0)
		{
			foreach($query->result() as $idnopo)
			{
				$tmpnopo = ((int)$idnopo->kdnopo)+1;
				$kd = sprintf("%04s", $tmpnopo);
			}
			
		}
		else
		{
			$kd = "0001";
		}
		
		$kd = "PO".$kd; 
		
		return $kd;
	}
	
	function getdetailid()
	{
		$querydet = $this->db->query("select MAX(IDdetail) as detid from transaction_order_product");
		$kd = "";
		
		if($querydet-> num_rows() > 0)
		{
			foreach($querydet->result() as $iddet)
			{
				$kd = ((int)$iddet->detid)+1;
			}
			
		}
		else
		{
			$kd = "1";
		}
		
		$kd = $kd; 
		
		return $kd;
	}
	
	function insert()
	{		
		$data = array(
			'NoPo' => $this->getNoPo(),
			'Tanggalpo' => $this->getTanggalpo(),
			'Kodesupplier' => $this->getKodesupplier(),
			'TotalQuantity' => $this->getTotalQuantity(),
			'TotalAmount' => $this->getTotalAmount(),
			'Keterangan' => $this->getKeterangan(),
			'Createdby' => $this->getCreatedBy(),
			'Createdon' => $this->getCreatedOn(),
			'Lastupdateby' => 'null',
			'Lastupdateon' => 'null',
			'Active' => '1'						
		);
		$this->db->insert('order_product',$data);
	}
	
	function insertdetail()
	{
		$data1 = array(
			'NoPo' => $this->getNoPo(),
			'IDdetail' => $this->getIDdetail(),
			'KodeDetailBarang' => $this->getKodeDetailBarang(),
			'HargaBarang' => $this->getHargaBarang(),
			'Quantity' => $this->getQuantity(),
			'Discount' => $this->getDiscount(),
			'Ppn' => '0',
			'Amount' => $this->getAmount()					
		);
		$this->db->insert('transaction_order_product',$data1);
	}
	
	function ubahpembelian(){
		$data = array(
				'TotalQuantity' => $this->getTotalQuantity(),
				'TotalAmount' => $this->getTotalAmount(),
				'Keterangan' => $this->getKeterangan(),
				'Lastupdateby' => $this->getLastupdateby(),
				'Lastupdateon' => $this->getLastupdateon()
                
            );
 
        $this->db->where(array('NoPo' => $this->getNoPo()));
        $this->db->update('order_product', $data);		
	}
	
	function ubahpembeliandet(){
		
		$data1 = array(
		
			'KodeDetailBarang' => $this->getKodeDetailBarang(),
			'HargaBarang' => $this->getHargaBarang(),
			'Quantity' => $this->getQuantity(),
			'Discount' => $this->getDiscount(),
			'Ppn' => '0',
			'Amount' => $this->getAmount()   
		);
 		
        $this->db->where(array('IDdetail' => $this->getIDdetail()));
        $this->db->update('transaction_order_product', $data1);		
	}
	
	function deletepembelian() {
        $data = array(
            'Active' => '0'
        );
        $this->db->where(array('NoPo' => $this->getNoPo()));
        $this->db->update('order_product', $data);
    }
	
	function getkodepono(){
		$tanggal=date('Y-m-d', strtotime('now'));
		$this->db->select("order_product.NoPo,order_product.Tanggalpo,order_product.Kodesupplier,supplier.suppliername,order_product.Keterangan,transaction_order_product.KodeDetailBarang,detail_product.Productname,transaction_order_product.HargaBarang,transaction_order_product.Quantity,transaction_order_product.Discount,transaction_order_product.Amount,transaction_order_product.IDdetail")->join('order_product', 'transaction_order_product.NoPo=order_product.NoPo')->join('supplier', 'supplier.Kodesupplier=order_product.Kodesupplier')->join('detail_product', 'detail_product.KodeDetailBarang=transaction_order_product.KodeDetailBarang')
		->where(array('order_product.NoPo' => $this->getNoPo(),'transaction_order_product.IDdetail' => $this->getIDdetail(),'order_product.Active'=>'1'));
        $query = $this->db->get('transaction_order_product');
        return $query->result();
	}
	
	function getList(){
		$tanggal=date('Y-m-d', strtotime('now'));
		$this->db->select("order_product.NoPo,order_product.Tanggalpo,order_product.Kodesupplier,supplier.suppliername,order_product.Keterangan,transaction_order_product.KodeDetailBarang,detail_product.Productname,transaction_order_product.HargaBarang,transaction_order_product.Quantity,transaction_order_product.Discount,transaction_order_product.Amount,transaction_order_product.IDdetail")->join('transaction_order_product', 'transaction_order_product.NoPo=order_product.NoPo')->join('supplier', 'supplier.Kodesupplier=order_product.Kodesupplier')->join('detail_product', 'detail_product.KodeDetailBarang=transaction_order_product.KodeDetailBarang')
        ->where(array('order_product.Active' =>'1'));
		$this->db->order_by('order_product.NoPo', 'asc');
        $query = $this->db->get('order_product');
        return $query->result();
	}
	
	function countGetList(){
		$tanggal=date('Y-m-d', strtotime('now'));
		$this->db->select("order_product.NoPo,order_product.Tanggalpo,order_product.Kodesupplier,supplier.suppliername,order_product.Keterangan,transaction_order_product.KodeDetailBarang,detail_product.Productname,transaction_order_product.HargaBarang,transaction_order_product.Quantity,transaction_order_product.Discount,transaction_order_product.Amount,transaction_order_product.IDdetail")->join('transaction_order_product', 'transaction_order_product.NoPo=order_product.NoPo')->join('supplier', 'supplier.Kodesupplier=order_product.Kodesupplier')->join('detail_product', 'detail_product.KodeDetailBarang=transaction_order_product.KodeDetailBarang')
        ->where(array('order_product.Active' =>'1'));
		$this->db->order_by('order_product.NoPo', 'asc');
        $query = $this->db->get('order_product');
        return $query->num_rows();
	}
	
	function getListlimit($limit,$start){
		$tanggal=date('Y-m-d', strtotime('now'));
		$this->db->select("order_product.NoPo,order_product.Tanggalpo,order_product.Kodesupplier,supplier.suppliername,order_product.Keterangan,transaction_order_product.KodeDetailBarang,detail_product.Productname,transaction_order_product.HargaBarang,transaction_order_product.Quantity,transaction_order_product.Discount,transaction_order_product.Amount,transaction_order_product.IDdetail")->join('transaction_order_product', 'transaction_order_product.NoPo=order_product.NoPo')->join('supplier', 'supplier.Kodesupplier=order_product.Kodesupplier')->join('detail_product', 'detail_product.KodeDetailBarang=transaction_order_product.KodeDetailBarang')
       	->where(array('order_product.Active' =>'1'))
		->limit($limit,$start);
		$this->db->order_by('order_product.NoPo', 'asc');
        $query = $this->db->get('order_product');
        return $query->result();
	}
	
	function gettranspono(){
		$this->db->select("order_product.NoPo,order_product.Tanggalpo,order_product.Kodesupplier,supplier.suppliername,
		order_product.Keterangan,transaction_order_product.KodeDetailBarang,detail_product.Productname,detail_product.Typeunit,
		transaction_order_product.HargaBarang,transaction_order_product.Quantity,transaction_order_product.Discount,
		transaction_order_product.Amount,transaction_order_product.IDdetail")
		->join('transaction_order_product', 'transaction_order_product.NoPo=order_product.NoPo')
		->join('supplier', 'supplier.Kodesupplier=order_product.Kodesupplier')
		->join('detail_product', 'detail_product.KodeDetailBarang=transaction_order_product.KodeDetailBarang')
		->where(array('order_product.NoPo' => $this->getNoPo(),'order_product.Active'=>'1'));
        $query = $this->db->get('order_product');
        return $query->result();
	}
	
	function getlistpertanggal(){
		$this->db->select("order_product.NoPo,order_product.Tanggalpo,order_product.Kodesupplier,supplier.suppliername,order_product.Keterangan,transaction_order_product.KodeDetailBarang,detail_product.Productname,transaction_order_product.HargaBarang,transaction_order_product.Quantity,transaction_order_product.Discount,transaction_order_product.Amount,transaction_order_product.IDdetail")->join('transaction_order_product', 'transaction_order_product.NoPo=order_product.NoPo')->join('supplier', 'supplier.Kodesupplier=order_product.Kodesupplier')->join('detail_product', 'detail_product.KodeDetailBarang=transaction_order_product.KodeDetailBarang')
        ->where (array('order_product.Tanggalpo BETWEEN '.$this->getTanggalAwal().' AND '.$this->getTanggalAkhir().'','order_product.Active' =>'1'));
        $query = $this->db->get('order_product');
        return $query->result();
	}
	
	function getListpono(){
		$tanggal=date('Y-m-d', strtotime('now'));
		$this->db->select("order_product.NoPo,order_product.Tanggalpo,order_product.Kodesupplier,supplier.suppliername,order_product.Keterangan,transaction_order_product.KodeDetailBarang,detail_product.Productname,transaction_order_product.HargaBarang,transaction_order_product.Quantity,transaction_order_product.Discount,transaction_order_product.Amount,transaction_order_product.IDdetail")->join('transaction_order_product', 'transaction_order_product.NoPo=order_product.NoPo')->join('supplier', 'supplier.Kodesupplier=order_product.Kodesupplier')->join('detail_product', 'detail_product.KodeDetailBarang=transaction_order_product.KodeDetailBarang')
                ->where(array('order_product.Active'=>'1'))->like(array('order_product.NoPo' => $this->getNoPo()));
        $query = $this->db->get('order_product');
        return $query->result();
	}
	
	
	function getListkodesupp(){
		$tanggal=date('Y-m-d', strtotime('now'));
		$this->db->select("order_product.NoPo,order_product.Tanggalpo,order_product.Kodesupplier,supplier.suppliername,order_product.Keterangan,transaction_order_product.KodeDetailBarang,detail_product.Productname,transaction_order_product.HargaBarang,transaction_order_product.Quantity,transaction_order_product.Discount,transaction_order_product.Amount,transaction_order_product.IDdetail")->join('transaction_order_product', 'transaction_order_product.NoPo=order_product.NoPo')->join('supplier', 'supplier.Kodesupplier=order_product.Kodesupplier')->join('detail_product', 'detail_product.KodeDetailBarang=transaction_order_product.KodeDetailBarang')
		->where(array('order_product.Active'=>'1'))->like(array('supplier.suppliername' => $this->getsuppliername()));
        $query = $this->db->get('order_product');
        return $query->result();
	}
	
	function getListkodebarang(){
		$tanggal=date('Y-m-d', strtotime('now'));
		$this->db->select("order_product.NoPo,order_product.Tanggalpo,order_product.Kodesupplier,supplier.suppliername,order_product.Keterangan,transaction_order_product.KodeDetailBarang,detail_product.Productname,transaction_order_product.HargaBarang,transaction_order_product.Quantity,transaction_order_product.Discount,transaction_order_product.Amount,transaction_order_product.IDdetail")->join('transaction_order_product', 'transaction_order_product.NoPo=order_product.NoPo')->join('supplier', 'supplier.Kodesupplier=order_product.Kodesupplier')->join('detail_product', 'detail_product.KodeDetailBarang=transaction_order_product.KodeDetailBarang')
		->where(array('order_product.Active'=>'1'))->like(array('detail_product.Productname' => $this->getProductname()));
        $query = $this->db->get('order_product');
        return $query->result();
	}
	
	function getTotalqty()
	{
		
		$query = $this->db->query("select sum(Quantity) as totalquant from transaction_order_product
			where NoPo='". $this->getNoPo() ."'");
		$row = $query->row();
		return $row->totalquant;
	}
	
	function getTotalamt()
	{
		$query = $this->db->query("select sum(Amount) as TotalAmount from transaction_order_product
			where NoPo='". $this->getNoPo() ."'");
		$row = $query->row();
		return $row->TotalAmount;
	}
	
	function ambilnoPO(){
		$this->db->select("order_product.NoPo,order_product.Kodesupplier,supplier.suppliername,
		order_product.Tanggalpo")
		->join('supplier', 'supplier.Kodesupplier=order_product.Kodesupplier')
		->where(array('order_product.NoPo' => $this->getNoPo(),'order_product.Active'=>'1'));
		$query = $this->db->get('order_product');
        return $query->result();
	}
	
}